package log;

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Calendar;

import objects.AlarmMakingNoise;
import objects.AlarmSnoozing;

/**
 * Aspecto para la definicion de un registro (o log).
 * 
 * @author Carlos Rodríguez Bollaín <carlos.rodriguezb@alumnos.unican.es>
 * @author Sergio Salomón García <sergio.salomon@alumnos.unican.es>
 * @author Antonio Minondo Tshuma <antonio.minondo@alumnos.unican.es>
 */
public aspect Logger {
	private static String logFile = "log.txt";
	private PrintWriter out;

	private long timeMakingNoise, timeSnoozing;

	public Logger() {
		try {
			out = new PrintWriter(logFile);
		} catch (FileNotFoundException e) {
			System.out.println("Error al crear el fichero " + logFile);
			e.printStackTrace();
		}
	}

	/**
	 * The moment the alarm starts running.
	 */
	pointcut alarmMakingNoiseExecution() : execution(* AlarmMakingNoise.run());

	before() : alarmMakingNoiseExecution(){
		Calendar c = Calendar.getInstance();
		timeMakingNoise = c.getTimeInMillis();
	}

	/**
	 * Whenever snooze is executed.
	 */
	pointcut alarmSnoozingExecution() : execution(* AlarmSnoozing.snooze(..));

	/*
	 * Before it is executed, we store the current time.
	 */
	before() : alarmSnoozingExecution(){
		Calendar c = Calendar.getInstance();
		timeSnoozing = timeSnoozing + c.getTimeInMillis();
	}

	/*
	 * And after it is executed, we store it again, subtracting the value, to
	 * get only the milliseconds spent snoozing.
	 */
	after() : alarmSnoozingExecution(){
		Calendar c = Calendar.getInstance();
		timeSnoozing = c.getTimeInMillis() - timeSnoozing;
	}

	/**
	 * The moment the alarmStop method is executed.
	 */
	pointcut alarmStopExecution() : execution(* AlarmMakingNoise.stop());

	/*
	 * Calculate the time spent making noises and log it.
	 */
	before() : alarmStopExecution(){

		Calendar c = Calendar.getInstance();
		timeMakingNoise = c.getTimeInMillis() - timeMakingNoise;
		long timeSpent = timeMakingNoise - timeSnoozing;

		// Calculates the initial time.
		Calendar date = Calendar.getInstance();
		date.roll(Calendar.MILLISECOND, -(int) timeMakingNoise);

		// Sets up a calendar that stores the time spent in a Calendar.
		Calendar ts = Calendar.getInstance();
		ts.clear();
		ts.set(Calendar.MILLISECOND, (int) timeSpent);

		// Write to the log file.
		writeLog(getLogString(date, ts));
	}

	private String getLogString(Calendar date, Calendar timeSpent) {
		String log = date.get(Calendar.YEAR) + "/" + date.get(Calendar.MONTH)
				+ "/" + date.get(Calendar.DATE) + "	Activada a las "
				+ date.get(Calendar.HOUR_OF_DAY) + ":"
				+ date.get(Calendar.MINUTE) + "	Sonando durante: "
				+ timeSpent.get(Calendar.MINUTE) + "'";
		if (timeSpent.get(Calendar.SECOND) < 10)
			log = log + "0" + timeSpent.get(Calendar.SECOND);
		else
			log = log + timeSpent.get(Calendar.SECOND);
		return log;
	}

	private void writeLog(String text) {
		out.println(text);
		out.flush();
		// System.out.println("DEBUG: " + text);
	}

}
